<?
function ShowListFull()
{
	global $db, $table_edit, $arr, $elements, $db_name;
	
	$sql = "select column_name from information_schema.columns where table_name='".$table_edit."' AND table_schema ='".$db_name."'" ;
	
	$arr = $db->getAll($sql);
	
	$elements = ShowListArr($table_edit);
}

function EditFull()
{
	global $db, $table_edit, $arr_table, $element, $db_name;
	
	$sql = "select column_name, data_type from information_schema.columns where table_name='".$table_edit."'  AND table_schema ='".$db_name."'";
	
	$arr_table = $db->getAll($sql);
	
	$element = EditElement($table_edit);
}
function ChangeActive($name)
{
	global $table_edit;
	
	ChangeMethod($table_edit, $name);
	
}
function ChangeShowList($name)
{
	global $table_edit;
	
	ChangeShow($table_edit, $name);
}
function ChangeHideList($name)
{
	global $table_edit;
	
	ChangeHide($table_edit, $name);
}
function Order()
{
	global $table_edit;
	OrderList($table_edit);
}

function DelList()
{
	global $table_edit;
	
	DelListElement($table_edit);
}

function Del()
{
	global $table_edit;
	
	DelElement($table_edit);
}

function PostClean($name)
{
	$value = isset($_POST[$name])?$_POST[$name]:'';
	
	if($value)
		return $value;
	return '';	
}
function GetClean($name)
{
	$value = isset($_GET[$name])?$_GET[$name]:'';
	
	if($value)
		return $value;
	return '';	
}
function ArrPostClean($arr, $name)
{
	$value = isset($_POST[$name])?$_POST[$name]:'';
	
	if($value)
		$arr[$name] = $value;
	return $arr;	
}
function ArrGetClean($arr, $name)
{
	$value = isset($_GET[$name])?$_GET[$name]:'';
	
	if($value)
		$arr[$name] = $value;
	return $arr;	
}
function ArrPostClean2($arr, $name, $name2)
{
	$value = isset($_POST[$name2])?$_POST[$name2]:'';
	
	if($value)
		$arr[$name] = $value;
	return $arr;	
}
function ArrGetClean2($arr, $name, $name2)
{
	$value = isset($_GET[$name2])?$_GET[$name2]:'';
	
	if($value)
		$arr[$name] = $value;
	return $arr;	
}
function ArrPostCleanActive($arr, $name)
{
	$value = isset($_POST[$name])?$_POST[$name]:'';
	
	$value = ($value == 'on')?'1':'0';
	
	$arr[$name] = $value;
		
	return $arr;	
}
function ShowListArr($table_edit)
{
	global $db, $page, $plpage, $set_per_page, $c;
	$set_per_page=20; 
	if(isset($_GET['cid']) && $_GET['cid']!=1 && $_GET['cid']!='')
		$sql="select p.* from ".$table_edit." p where p.cid=".$_GET['cid']." order by p.num asc, p.id desc ";
	else
		$sql="select * from ".$table_edit." order by num asc, id desc ";
	//echo $sql;	
	$c = $db->numRows($db->query($sql));
	$plpage = plpage($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);	
	return $db->getAll($sqlstmt);	
}
function EditElement($table_edit)
{
	global $db;
	
	if($_GET['act'] == 'edit_full')
	{
		$id=$_GET["id"];
		$sql = "select * from ".$table_edit." where id=$id";
		return $db->getRow($sql);
	}
}
function OrderList($table_edit)
{
	global $db;
	$id=$_POST["id"];	
	$ordering=$_POST["ordering"];		
	for($i=0;$i<count($id);$i++){
		$sql="update ".$table_edit." set num=".$ordering[$i]." where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Order successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin-pro.html?do=".$table_edit. (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function ChangeMethod($table_edit, $name)
{
	global $db;
	$id=$_GET["id"];
	$cid = isset($_GET["cid"])?$_GET["cid"]:'';
	
	$active = $_GET['current']==1?0:1;
	$sql="update ".$table_edit." set ".$name."=".$active." where id=$id";
	$db->query($sql);	
	
	$msg="Update successfully";	
		
	if(!empty($_GET['cid']))
		$page="admin-pro.html?do=".$table_edit.(isset($_GET['cid'])?'&cid='.$_GET['cid']:''). "&page=" . ($_GET['page']!=""?$_GET['page']:1);
	else
		$page="admin-pro.html?do=".$table_edit;
	$_SESSION['mess'] = $msg;
	page_transfer2($page);	
}
function ChangeShow($table_edit, $name)
{
	global $db;
	$id=$_POST["iddel"];
	for($i=0;$i<count($id);$i++){
		$sql="update ".$table_edit." set ".$name." = 1 where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Update successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin-pro.html?do=".$table_edit. (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function ChangeHide($table_edit, $name)
{
	global $db;
	$id=$_POST["iddel"];
	for($i=0;$i<count($id);$i++){
		$sql="update ".$table_edit." set ".$name." =0 where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Update successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin-pro.html?do=".$table_edit. (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function DelListElement($table_edit)
{
	global $db;
	$id=$_POST["iddel"];		
	for($i=0;$i<count($id);$i++){
		$sqlstmt="select img from ".$table_edit." where id=".$id[$i];
		$r = $db->getRow($sqlstmt);
		if(file_exists($r['img'])) unlink($r['img']);		
				
		$sql="delete from ".$table_edit." where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Delete successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin-pro.html?do=".$table_edit. (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function DelElement($table_edit)
{
	global $db;
	$id=$_GET["id"];

	$sqlstmt="select img from ".$table_edit." where id=$id";
	$r = $db->getRow($sqlstmt);
	if(file_exists($r['img'])) unlink($r['img']);		
	
	$sql="delete from ".$table_edit."  where id=".$id;
	$db->query($sql);	
		
	$msg="Delete successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin-pro.html?do=".$table_edit. (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function DownloadImage($str)
{
	global $FullUrl;
	$arrImageWeb = GetLinkImagesFromWebOther($str);
	//print_r($arrImageWeb);
	$dir = $FullUrl."kam/upload/images/webOthers/";
	$arrImageHost = ChangeImageName($arrImageWeb, $dir);		
	//print_r($arrImageHost);
	
	for($i = 0; $i < count($arrImageWeb); $i++)
	{
		DownloadImageFromWeb($arrImageWeb[$i], $arrImageHost[$i]);	
	}
			
	for($i = 0; $i < count($arrImageHost); $i++)		
	{
		$arrImageHost[$i] = "/".$arrImageHost[$i];	
	}
	$str = str_replace($arrImageWeb, $arrImageHost, $str);
	
	return $str;
}
function DownloadImageFromWeb($url, $save)
{
	file_put_contents($save, file_get_contents($url));	
}
function ChangeImageName($arrImageWeb, $dir)
{		
	
	$index = 0;
	
	for($i = 0; $i < count($arrImageWeb); $i++)	
	{
		$name = GetImageName($arrImageWeb[$i]);
		$rs[$index] = $dir.$name;
		$index++;	
	}
	
	return $rs;
}
function GetImageName($str)
{
	$rs = "";
	$index = 0;
	
	for($i = strlen($str)-1; $i > 0 ; $i--)
	{
		if($str[$i] != "/")
			$index = $i;		
		else
			break;	
	}	
	
	for($i = $index; $i < strlen($str); $i++)
		$rs .= $str[$i];
	
	return $rs;
}
function GetLinkImagesFromWebOther($str)
{
	$split = "<img"; 
	$arr = (explode($split, $str));
	$index = 0;
	$result = "";
	
	for($i = 0; $i < count($arr); $i++)
	{			
		if($arr[$i] != "" || $arr[$i] != " ")
		{				
			$split2 = 'src=\"'; 			
			$arr2 = (explode($split2, $arr[$i]));		
			
			if(count($arr2) > 1)
			{
				$rs = $arr2[1];
				$link = "";
				for($j = 0; $j < strlen($rs) - 1; $j++)
				{
					if($rs[$j] != "\\" && $rs[$j+1] != '"')
					{
						$link .= $rs[$j];
					}
					else
						break;
				}
				
				if($link != "")
				{
					$result[$index] = $link;			
					$index++;
				}
	
			}
		}
	}
	
	//print_r($result);
	//echo "END";
	return $result;
	
}
?>